package com.example.administrator.asdasd;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

;

public class MainActivity extends FragmentActivity implements View.OnClickListener {

    public static FragmentActivity Mythis;
    public static String name;
    private ViewPager viewPager;
    private FragmentPagerAdapter madpater;
    private List<Fragment> mdatas;
    private Button mtv1;
    private Button mtv2;
    private Button mtv3;
    private Button mtv4;

    private ImageView mTabline;
    private int mScreen1_4;
    private int mCurrentPageIndex;
    private int flagt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Mythis = this;
        name = getIntent().getStringExtra("name");

        intitTabLine();
        inview();

    }

    private void intitTabLine() {
        mTabline = (ImageView) findViewById(R.id.end_tab);
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        mScreen1_4 = outMetrics.widthPixels / 4;
        LayoutParams lp = mTabline.getLayoutParams();
        lp.width = mScreen1_4;
        mTabline.setLayoutParams(lp);
    }

    private void inview() {
        viewPager = (ViewPager) findViewById(R.id.id_vpager);
        mtv1 = (Button) findViewById(R.id.end_hz);
        mtv2 = (Button) findViewById(R.id.end_cz);
        mtv3 = (Button) findViewById(R.id.end_sc);
        mtv4 = (Button) findViewById(R.id.end_gr);

        mtv1.setOnClickListener(this);
        mtv2.setOnClickListener(this);
        mtv3.setOnClickListener(this);
        mtv4.setOnClickListener(this);

        mdatas = new ArrayList<Fragment>();
        Fragment_hz tab01 = new Fragment_hz();
        Fragment_cz tab02 = new Fragment_cz();
        Fragment_sc tab03 = new Fragment_sc();
        Fragment_gr tab04 = new Fragment_gr();

        mdatas.add(tab01);
        mdatas.add(tab02);
        mdatas.add(tab03);
        mdatas.add(tab04);
        madpater = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int arg0) {
                return mdatas.get(arg0);
            }

            @Override
            public int getCount() {
                return mdatas.size();
            }
        };
        viewPager.setAdapter(madpater);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                resetTextView();
                switch (position) {
                    case 0:
                        mtv1.setTextColor(Color.GREEN);
                        break;
                    case 1:
                        mtv2.setTextColor(Color.GREEN);
                        break;
                    case 2:
                        mtv3.setTextColor(Color.GREEN);
                        break;
                    case 3:
                        mtv4.setTextColor(Color.GREEN);
                        break;
                }
                mCurrentPageIndex = position;
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPx) {

                LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabline
                        .getLayoutParams();

                if (mCurrentPageIndex == 0 && position == 0)// 0->1
                {
                    lp.leftMargin = (int) (positionOffset * mScreen1_4 + mCurrentPageIndex
                            * mScreen1_4);
                } else if (mCurrentPageIndex == 1 && position == 0)// 1->0
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4 + (positionOffset - 1)
                            * mScreen1_4);
                } else if (mCurrentPageIndex == 1 && position == 1) // 1->2
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4 + positionOffset
                            * mScreen1_4);
                } else if (mCurrentPageIndex == 2 && position == 1) // 2->1
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4 + (positionOffset - 1)
                            * mScreen1_4);
                }else if (mCurrentPageIndex == 2 && position == 3) // 2->3
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4 + positionOffset
                            * mScreen1_4);
                } else if (mCurrentPageIndex == 3 && position == 2) // 3->2
                {
                    lp.leftMargin = (int) (mCurrentPageIndex * mScreen1_4 + (positionOffset - 1)
                            * mScreen1_4);
                }
                mTabline.setLayoutParams(lp);

            }


            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.end_hz:
                setSelect(0);
                break;
            case R.id.end_cz:
                setSelect(1);
                break;
            case R.id.end_sc:
                setSelect(2);
                break;
            case R.id.end_gr:
                setSelect(3);
                break;
        }
    }

    public void setSelect(int i) {
        resetTextView();
        viewPager.setCurrentItem(i);
        switch (i) {
            case 0:
                mtv1.setTextColor(Color.GREEN);
                break;
            case 1:
                mtv2.setTextColor(Color.GREEN);
                break;
            case 2:
                mtv3.setTextColor(Color.GREEN);
                break;
            case 3:
                mtv4.setTextColor(Color.GREEN);
                break;
        }
    }

    private void resetTextView() {
        mtv1.setTextColor(Color.BLACK);
        mtv2.setTextColor(Color.BLACK);
        mtv3.setTextColor(Color.BLACK);
        mtv4.setTextColor(Color.BLACK);
    }

}
